const phones = [
    {
        brand: 'huawei',
        version: 'mate40',
        price: 5998,
        sales: 3123,
    },
    {
        brand: 'huawei',
        version: 'p50',
        price: 6998,
        sales: 12323,
    },
    {
        brand: 'huawei',
        version: 'p40',
        price: 4998,
        sales: 8981,
    },
    {
        brand: 'apple',
        version: '13pro',
        price: 9999,
        sales: 8982,
    },
    {
        brand: 'apple',
        version: '13',
        price: 7999,
        sales: 9312,
    },
    {
        brand: 'sumsing',
        version: 's12',
        price: 10999,
        sales: 123,
    },
    {
        brand: 'oppo',
        version: 'find100',
        price: 7999,
        sales: 4122,
    },
    {
        brand: 'mi',
        version: '11',
        price: 4999,
        sales: 10123,
    },
    {
        brand: 'mi',
        version: 'mix4',
        price: 8499,
        sales: 2141,
    },
];

const madeInChina = [
    'oppo',
    'mi',
    'huawei',
];

// 求国产手机的平均售价
const phonesMadeInChina = phones.filter((phone) => {
    return madeInChina.includes(phone.brand);
});

console.log(
    phonesMadeInChina.reduce((prevResult, current) => {
        return prevResult + current.price;
    }, 0) / phonesMadeInChina.length
);
// 求华为手机的平均售价
const huaweis = phones.filter((phone) => {
    return phone.brand === 'huawei';
});

console.log(
    huaweis.reduce((prevResult, current) => {
        return prevResult + current.price;
    }, 0) / huaweis.length
);
// 求国产手机的销量排行榜 如：['huawei p50', 'mi 11', 'huawei p40']
console.log(
    phonesMadeInChina
        .sort((a, b) => b.sales - a.sales)
        .map((phone) => {
            return `${ phone.brand } ${ phone.version }`;
        })
);
